﻿using System;
using System.Collections.Generic;
using System.Collections;
using NHibernate.Cfg;
using NHibernate;


namespace Model {
  
  public class Usuario
  {

    private string idUsuario;
    private string login;
    private string senha;

    #region Propriedades
    public string IdUsuario {
      get { return idUsuario; }
      set { idUsuario = value; }
    }

    

    public string Login {
      get { return login; }
      set { login = value; }
    }

  

    public string Senha {
      get { return senha; }
      set { senha = value; }
    }
    #endregion


    #region Metodos

    #region Persist
    public Boolean Persist()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Save(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Update
    public Boolean Update()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Update(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Delete
    public static Boolean Delete(int pIdUsuario)
    {
        Usuario objUsuario = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objUsuario = (Usuario)objSession.Load(typeof(Model.Usuario), pIdUsuario);
            objSession.Delete(objUsuario);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region RetriveObject
    public static Usuario RetriveObject(string pIdUsuario)
    {
        Usuario objUsuario = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objUsuario = (Usuario)objSession.Load(typeof(Model.Usuario), pIdUsuario);
            objSession.Close();
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
        return objUsuario;
    }
    #endregion

    #region RetriveObjects
    public static IList RetriveObjects()
    {
        IList listUsuarios;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            listUsuarios = objSession.CreateCriteria(typeof(Model.Usuario)).List();
            objSession.Close();
            
        }
        catch (Exception ex)
        {
            listUsuarios = null;
            objTransaction.Rollback();
            throw ex;
        }
        return listUsuarios;
    }
    #endregion

    #endregion

  }
}
